home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Transform / Quaternion.h < prev   
C/C++ Source or Header  |  1992-07-14  |  6KB  |  185 lines

  1. //
  2. // Copyright (C) 1992 General Electric Company.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // General Electric Company,
  10. // provides this software "as is" without express or implied warranty.
  11. //
  12. // Created: VDN 06/23/92 -- design and implementation
  13. //
  14. // Quaternion is a vector with 1 real and 3 imaginary parts:
  15. //       q = cos(theta/2) + sin(theta/2) (x i + y j + z k)
  16. // Quaternion is represented by a 4-elmt vector, with imaginary parts
  17. // being the first 3 elements, so that they are aligned with x, y, z, and
  18. // the real part, the fourth element.
  19. //
  20. // References:
  21. // 1. Horn, B.K.P. (1987) Closed-form solution of absolute orientation using 
  22. //       unit quaternions. J. Opt. Soc. Am. Vol 4, No 4, April.
  23. // 2. Horn, B.K.P. (1987) Robot Vision. MIT Press. pp. 437-551.
  24.  
  25. #ifndef QUATERNIONH
  26.  
  27. #ifndef MATRIXH
  28. #include <cool/Matrix.h>
  29. DECLARE CoolMatrix<float>;            // need only forward decl.
  30. #endif
  31.  
  32. #ifndef M_VECTORH
  33. #include <cool/M_Vector.h>
  34. DECLARE CoolM_Vector<float>;            // include parent class
  35. #endif
  36.  
  37. class CoolQuaternionE;                // forward decl of envelope
  38.  
  39. class CoolQuaternion : public CoolM_Vector<float> {
  40.  public:
  41.   CoolQuaternion (float x = 0, float y = 0, float z = 0, // default is null quat.
  42.           float r = 1.0); 
  43.   CoolQuaternion (const CoolM_Vector<float>& axis, // from axis&angle
  44.           float angle);            
  45.   CoolQuaternion (const CoolMatrix<float>& transform); // from 2-4 square row-major
  46.   inline CoolQuaternion (const CoolM_Vector<float>& vec); // from 2-4D vector
  47.   inline CoolQuaternion (const CoolQuaternion& q);      // copy constructor
  48.   inline ~CoolQuaternion();                  // free internal array
  49.   
  50.   inline float& x ();                // imaginary component
  51.   inline float& y ();                // parallel to axis of rotation
  52.   inline float& z ();
  53.   inline float& r ();                // real component
  54.   inline float real () const;                
  55.   inline CoolM_Vector<float>E imaginary () const; // imaginary vector part
  56.   
  57.   CoolM_Vector<float>E axis () const;        // Axis of rotation
  58.   float angle () const;                // Angle of rotation
  59.   
  60.   inline CoolQuaternion& operator= (const CoolQuaternion& rhs);    // q1 = q2
  61.   inline CoolQuaternion& operator= (CoolQuaternionE& env);    // q1 = env
  62.   
  63.   Boolean operator== (const CoolQuaternion& rhs) const; // cmp with fuzz = 1.0e-6
  64.   inline Boolean operator!= (const CoolQuaternion& rhs) const; // instead of 1.0e-8
  65.   
  66.   inline operator CoolM_Vector<float>& ();    // cast to 4-elmt vector
  67.   CoolMatrix<float>E rotation_transform (int dim = 4) const; // to 2-4 rot matrix
  68.   
  69.   CoolQuaternionE conjugate () const;        // same real, opposite img part
  70.   CoolQuaternionE inverse () const;        // inverse for nonzero quat
  71.   
  72.   friend CoolQuaternionE operator* (const CoolQuaternion& q1, // q = q1 * q2 * q3
  73.                     const CoolQuaternion& q2);
  74.   void rotate (CoolM_Vector<float>& v) const;    // rotate 3D v, store result in v.
  75.   
  76.   inline friend ostream& operator<< (ostream& os, const CoolQuaternion& q);
  77.   inline friend ostream& operator<< (ostream& os, const CoolQuaternion* q);
  78. };
  79.  
  80.  
  81. #define CoolEnvelope CoolQuaternionE
  82. #define CoolLetter CoolQuaternion
  83. #include <cool/Envelope.h>            // use Envelope to avoid deep
  84. #undef CoolEnvelope                // copies on return by value
  85. #undef CoolLetter
  86.  
  87. // Quaternion -- Construct Quaternion from 2-, 3- or 4-elmt vector.
  88. //             If nD-vector, construct imaginary Quaternion.
  89. // Input:      2D, 3D location vector, or 4D Quaternion vector.
  90.  
  91. inline CoolQuaternion::CoolQuaternion (const CoolM_Vector<float>& vec)
  92. : CoolM_Vector<float>(vec) {            // 1-1 layout between vector&quat
  93. }
  94.  
  95. // Quaternion -- Copy constructor
  96. // Input:      Quaternion
  97.  
  98. inline CoolQuaternion::CoolQuaternion (const CoolQuaternion& q) 
  99.   : CoolM_Vector<float>(q) {            // 1-1 layout between vector&quat
  100. }
  101.  
  102. // ~Quaternion -- Nothing, since Quaternion has same data as Vector.
  103.  
  104. inline CoolQuaternion::~CoolQuaternion () {}    // Vector will free data
  105.  
  106. // x -- Access first imaginary component, along x axis.
  107.  
  108. inline float& CoolQuaternion::x () {
  109.   return this->data[0];
  110. }
  111.  
  112. // y -- Access second imaginary component, along y axis.
  113.  
  114. inline float& CoolQuaternion::y () {
  115.   return this->data[1];
  116. }
  117.  
  118. // z -- Access third imaginary component, along z axis.
  119.  
  120. inline float& CoolQuaternion::z () {
  121.   return this->data[2];
  122. }
  123.  
  124. // r -- Access real component
  125.  
  126. inline float& CoolQuaternion::r () {
  127.   return this->data[3];
  128. }
  129.  
  130. // real -- Get real part
  131.  
  132. inline float CoolQuaternion::real () const {
  133.   return this->data[3];
  134. }
  135.  
  136. // imaginary -- Get imaginary part
  137.  
  138. inline CoolM_Vector<float>E CoolQuaternion::imaginary () const {
  139.   return this->extract(3,0);
  140. }
  141.  
  142.   // operator=  -- Assignment q1 = q2;
  143. // Input:    Quaternion on rhs
  144. // Output:   Quaternion on lhs, with contents of Quaternion on rhs copied over.
  145.  
  146. inline CoolQuaternion& CoolQuaternion::operator= (const CoolQuaternion& rhs) {
  147.   CoolM_Vector<float>::operator=(rhs);        // same as copy vector part
  148.   return *this;
  149. }
  150.  
  151. // operator=  -- Assignment from an envelope back to real Quaternion
  152. //            Swap the contents over, rhs envelope will be deleted by compiler.
  153.  
  154. inline CoolQuaternion& CoolQuaternion::operator= (CoolQuaternionE& env) {
  155.   env.shallow_swap((CoolQuaternionE*)this, &env); // same physical layout
  156.   return *this;  
  157. }
  158.  
  159. // operator!= -- Components of Quaternion are compared with fuzz = 1.0e-6
  160.  
  161. inline Boolean CoolQuaternion::operator!= (const CoolQuaternion& rhs) const {
  162.   return (!operator==(rhs));
  163. }
  164.  
  165. // operator M_Vector  --  Automatic conversion to a 4-elmt vector.
  166. // Input:    *this, a Quaternion.
  167. // Output:   a reference to the 4-elmt vector, through a type-cast.
  168.  
  169. inline CoolQuaternion::operator CoolM_Vector<float>& () {
  170.   return *((CoolM_Vector<float>*) this);    // same physical space
  171. }
  172.  
  173. // operator<<  -- Print the components of Quaternion.
  174.  
  175.   inline ostream& operator<< (ostream& os, const CoolQuaternion& q) {
  176.   return os << *((CoolM_Vector<float>*) &q);
  177. }
  178.  
  179. inline ostream& operator<< (ostream& os, const CoolQuaternion* q) {
  180.   return os << *((CoolM_Vector<float>*) q);
  181. }
  182.  
  183.  
  184. #endif                        // QuaternionH
  185.